package com.sc.nft.dto;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyHatchConf;
import com.sc.nft.entity.vo.ecology.AdminSmeltingPageVO;
import com.sc.nft.entity.vo.ecology.HatchPageVO;
import com.sc.nft.entity.vo.merchantPledgeTask.AdminEcologyIncomeConfListVO;
import com.sc.nft.mapper.EcologyHatchConfMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * 生态-孵化-配置总表(sc_ecology_hatch_conf)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-10-13 17:22:11
 */
@Slf4j
@Repository
public class EcologyHatchConfDao extends ServiceImpl<EcologyHatchConfMapper, EcologyHatchConf> {

    public IPage<AdminSmeltingPageVO> getSmeltingPage(String collectionName, Integer pageNo, Integer pageSize) {
        return baseMapper.getSmeltingPage(new Page<>(pageNo, pageSize), collectionName);
    }

    public Boolean updateSmeltingConf(EcologyHatchConf ecologyHatchConf) {
        LambdaUpdateWrapper<EcologyHatchConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getHatchCycle()) && ecologyHatchConf.getHatchCycle() > 0), EcologyHatchConf::getHatchCycle, ecologyHatchConf.getHatchCycle());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getExpendCount()) && ecologyHatchConf.getExpendCount() > 0), EcologyHatchConf::getExpendCount, ecologyHatchConf.getExpendCount());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getSort()) && ecologyHatchConf.getSort().intValue() > 0), EcologyHatchConf::getSort, ecologyHatchConf.getSort());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getUpperLimit()) && ecologyHatchConf.getUpperLimit() > 0), EcologyHatchConf::getUpperLimit, ecologyHatchConf.getUpperLimit());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getDestroyCount()) && ecologyHatchConf.getDestroyCount() >= 0), EcologyHatchConf::getDestroyCount, ecologyHatchConf.getDestroyCount());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getHatchCount()) && ecologyHatchConf.getHatchCount() >= 0), EcologyHatchConf::getHatchCount, ecologyHatchConf.getHatchCount());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getRewardsCount()) && ecologyHatchConf.getRewardsCount() >= 0), EcologyHatchConf::getRewardsCount, ecologyHatchConf.getRewardsCount());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getAddCastCount()) && ecologyHatchConf.getAddCastCount() >= 0), EcologyHatchConf::getAddCastCount, ecologyHatchConf.getAddCastCount());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getTotalHatchCount()) && ecologyHatchConf.getTotalHatchCount() >= 0), EcologyHatchConf::getTotalHatchCount, ecologyHatchConf.getTotalHatchCount());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchConf.getAddReforgeCount()) && ecologyHatchConf.getAddReforgeCount() >= 0), EcologyHatchConf::getAddReforgeCount, ecologyHatchConf.getAddReforgeCount());
        wrapper.set(ObjectUtil.isNotNull(ecologyHatchConf.getIsShowRemainder()), EcologyHatchConf::getIsShowRemainder, ecologyHatchConf.getIsShowRemainder());
        wrapper.set(ObjectUtil.isNotNull(ecologyHatchConf.getCoefficient()), EcologyHatchConf::getCoefficient, ecologyHatchConf.getCoefficient());
        wrapper.set(ObjectUtil.isNotNull(ecologyHatchConf.getEcologyPointsId()), EcologyHatchConf::getEcologyPointsId, ecologyHatchConf.getEcologyPointsId());
        wrapper.eq(EcologyHatchConf::getId, ecologyHatchConf.getId());
        return update(new EcologyHatchConf(), wrapper);
    }

    public boolean updateSmeltinStatusgById(Long id, Boolean status) {
        LambdaUpdateWrapper<EcologyHatchConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EcologyHatchConf::getStatus, status);
        wrapper.eq(EcologyHatchConf::getId, id);
        return update(new EcologyHatchConf(), wrapper);
    }

    public boolean updateSmeltingSortById(Long id, Long sort) {
        LambdaUpdateWrapper<EcologyHatchConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EcologyHatchConf::getSort, sort);
        wrapper.eq(EcologyHatchConf::getId, id);
        return update(new EcologyHatchConf(), wrapper);
    }

    /**
     * 修改是否消耗共识
     *
     * @param id
     * @param consumptionConsensus
     * @return
     */
    public boolean updateConsumptionConsensusById(Long id, Boolean consumptionConsensus) {
        LambdaUpdateWrapper<EcologyHatchConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EcologyHatchConf::getConsumptionConsensus, consumptionConsensus);
        wrapper.eq(EcologyHatchConf::getId, id);
        return update(new EcologyHatchConf(), wrapper);
    }

    public boolean delConfById(Long id) {
        return removeById(id);
    }

    public Page<HatchPageVO> hatchPage(Long userId, Page page,Long userCreatorId) {
        return baseMapper.hatchPage(page, userId,userCreatorId);
    }

    public EcologyHatchConf getOneByCollectionId(Long collectionId) {
        LambdaUpdateWrapper<EcologyHatchConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchConf::getCollectionId, collectionId);
        wrapper.eq(EcologyHatchConf::getStatus, Boolean.TRUE);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public Page<AdminEcologyIncomeConfListVO> getPageByCollectionNameAndDaoName(String collectionName, String daoName, Integer pageNo, Integer pageSize) {
        return baseMapper.getPageByCollectionNameAndDaoName(new Page<>(pageNo, pageSize), collectionName, daoName);
    }

    /**
     * 获取此DAO下所有参与熔炼的藏品
     *
     * @param daoId
     * @return
     */
    public List<Long> getEcologyHatchCollectonListByDaoId(Long daoId) {
        return baseMapper.getEcologyHatchCollectonListByDaoId(daoId);
    }


    public List<Long> getDaoIdByConfCollectionId() {
        return baseMapper.getDaoIdByConfCollectionId();
    }

    public List<EcologyHatchConf> getListByStatus() {
        LambdaQueryWrapper<EcologyHatchConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchConf::getStatus, Boolean.TRUE);
        return list(wrapper);
    }

    public Boolean getIsExistByCollectionId(Long collectionId) {
        LambdaQueryWrapper<EcologyHatchConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchConf::getCollectionId, collectionId);
        return count(wrapper) == 0;
    }

    public Boolean getIsExistByCollectionIds(List<Long> collectionIds) {
        LambdaQueryWrapper<EcologyHatchConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(EcologyHatchConf::getCollectionId, collectionIds);
        return count(wrapper) == 0;
    }


    /**
     * 获取脚本所需要的熔炼配置
     *
     * @return
     */
    public List<EcologyHatchConf> getScriptNeedList(Long id) {
        return baseMapper.getScriptNeedList(id);
    }

    /**
     * 获取脚本所需要的熔炼配置
     *
     * @return
     */
    public List<EcologyHatchConf> getNeedList(Long id) {
        return baseMapper.getNeedList(id);
    }

    public List<EcologyHatchConf> getScriptEcologyHatchConfList() {
        return baseMapper.getScriptEcologyHatchConfList();
    }

    /**
     * 获取品牌熔炼藏品
     *
     * @param daoId
     * @return
     */
    public List<EcologyHatchConf> hatchCollectionList(String collectionName, Long daoId) {
        LambdaQueryWrapper<EcologyHatchConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(collectionName), EcologyHatchConf::getCollectionName, collectionName);
        wrapper.eq(Objects.nonNull(daoId) && !Objects.equals(daoId, 6L), EcologyHatchConf::getDaoId, daoId);
        wrapper.eq(EcologyHatchConf::getStatus, Boolean.TRUE);
        return list(wrapper);
    }


    public List<EcologyHatchConf> getAllList() {
        LambdaQueryWrapper<EcologyHatchConf> wrapper = new LambdaQueryWrapper<>();
        return list(wrapper);
    }

    public List<EcologyHatchConf> ListByCollectionId(List<Long> hatchCollectionIdList) {
        LambdaQueryWrapper<EcologyHatchConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(EcologyHatchConf::getCollectionId,hatchCollectionIdList);
        return list(wrapper);
    }
}